package org.su.mamamiya.core.ast;

import lombok.AllArgsConstructor;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangchuan on 2019-09-26 17:16
 */
@AllArgsConstructor
public class DocHelper {

    private AstProccessor astProccessor;


    /**
     * 得到类的注解
     *
     * @param element
     * @return
     */
    public String getClassDoc(Element element) {
        return astProccessor.getElementUtils().getDocComment(element);
    }

    /**
     * 得到字段注释
     *
     * @param element
     * @param name
     * @return
     */
    public String getFiledDoc(Element element, String name) {
        StringBuilder sb = new StringBuilder();
        element.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.FIELD).forEach(x -> {
            if (x.getSimpleName().toString().equals(name)) {
                sb.append(astProccessor.getElementUtils().getDocComment(x));
            }
        });
        return sb.toString().trim();
    }

    /**
     * 通过注释内容，得到参数的文档
     *
     * @param txt
     * @return
     */
    public  ParamDoc getParamDoc(String txt) {

        txt = txt.replaceAll("，", ",");
        String[] txts = txt.split(",");
        StringBuilder sb = new StringBuilder();
        boolean need = true;
        boolean read = false;

        for (int i = 0; i < txts.length; i++) {
            txt = txts[i];
            if (i < 3) {
                if (txt.equals("非必须")) {
                    need = false;
                    continue;
                }

                if (txt.equals("必须")) {
                    continue;
                }

                if (txt.equals("只读")) {
                    read = true;
                    continue;
                }
            }

            sb.append(",").append(txt);

        }
        sb = sb.deleteCharAt(0);
        return new ParamDoc(null, sb.toString(), need, read);
    }


    public  List<ParamDoc> getParamDoces(String doc) {
        BufferedReader reader = new BufferedReader(new StringReader(doc));
        String s;
        List<ParamDoc> paramDocs = new ArrayList<>();

        int index = 0;
        ParamDoc paramDoc = null;
        try {
            while ((s = reader.readLine()) != null) {
                if (s.trim().startsWith("@param")) {
                    s = s.trim().substring(7).trim();

                    index = s.indexOf(" ");
                    if (index < 0) {
                        index = 0;
                    }
                    String name = index == 0 ? s : s.substring(0, index);
                    paramDoc = getParamDoc(s.substring(index).trim());
                    paramDoc.setName(name);
                    paramDocs.add(paramDoc);
                } else if (s.trim().startsWith("@return")) {

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return paramDocs;
    }
}
